﻿/*{
author=pk2.2（기마책사、江东新风）
date=2021/12/05
}*/
// ## 2022/11/05 # 铃# 完全汉化,优化语句 ##
// ## 2021/12/05 # 江东新风 # 技巧不足时不出现 ##
// ## 2021/09/26 # 江东新风 # 语句优化2 ##
// ## 2021/02/17 # 江东新风 # 语句优化 ##
// ## 2020/11/17 # 江东新风 # trace 信息乱码修复 ##
// ## 2020/10/30 # 江东新风 # 同步马术书大神的更新 ##
// ## 2020/07/26 ##
/*
※ 주의사항 : 본 스크립트의 기본 COST 更改 적용 시 SCENARIO 파일에 설정된 값을 무시하고 설정됩니다.

@만든이: 기마책사
@Update: '19.1.10  / 최초적용: 技巧연구 필요값 설정更改 기능 추가,   매턴 技巧연구 우대기능 추가

*/

namespace 技巧研究设定
{
    //---------------------------------------------------------------------------------------
    // 用户设置（true = 开启，false = 关闭）
    // 更改默认的技巧研究设置（※ 使用前请注意确认！！）
    const bool 调试模式 = false;            // 是否在启动器日志窗口中显示更改信息
    //---------------------------------------------------------------------------------------
    // 在每个回合中以一定概率应用技巧研究优惠
    const bool 技巧_COST_优先设置 = true;
    const int 技巧消耗_优惠几率 = 30; // 0~100之间的值---由于加入了各种不出现的情况，稍微提高触发率
    //---------------------------------------------------------------------------------------
    class Main
    {
        // 技巧数值备份
        array<uint16> tech_cost_gold(技巧_末, 0);
        array<uint16> tech_cost_tp(技巧_末, 0);
        array<uint16> tech_cost_time(技巧_末, 0);
        Main()
        {
            pk::bind(102, pk::trigger102_t(onGameInit));
            pk::bind(111, pk::trigger111_t(onTurnStart));
            pk::bind(112, pk::trigger112_t(onTurnEnd));
            pk::bind(100, pk::trigger100_t(onGameTitle));
        }
        //---------------------------------------------------------------------------------------
        // 标题界面
        void onGameTitle()
        {
            onGameInit();
        }
        // 游戏初始化时应用用户设置值
        void onGameInit()
        {
            if (pk::is_campaign())
                return;
            // 备份技巧设置值
            backup_tech_cost();
        }
        //---------------------------------------------------------------------------------------
        // 回合结束时恢复技巧设置值
        void onTurnEnd(pk::force @force)
        {
            if (pk::is_campaign())
                return;
            reset_tech_cost();
        }
        // 回合开始时随机应用技巧优惠
        void onTurnStart(pk::force @force)
        {
            if (pk::is_campaign())
                return;
            if (!pk::is_normal_force(force))
                return;
            if (!技巧_COST_优先设置)
                return;
            // 检查是否有正在研究的技巧
            int tech_rnd = force.researching_tech;
            if (tech_rnd >= 0)
            {
                // 如果已经在研究，则不触发
                if (force.is_player())
                {
                    return;
                }
                return;
            }
            // 应用技巧优惠
            if (pk::rand_bool(技巧消耗_优惠几率))
            {
                int tech_group = pk::rand(9);                      // 从9个领域中随机选择
                int tech_id = get_next_tech_id(force, tech_group); // 返回选择领域中未研究阶段的技巧ID
                int adv_type = pk::rand(3);                        // 随机选择优化金币/技巧点/时间的优惠方式
                if (tech_id < 0)
                {
                    // 该领域已研究完毕，不触发
                    return;
                }
                else
                {
                    // 当技巧明显不足时，不触发
                    uint16 cost = pk::get_tech(tech_id).tp_cost;
                    if (force.tp < (cost - 50 - ((adv_type == 1) ? cost / 2 : 0)))
                        return;
                    string tech_name = pk::decode(pk::get_name(pk::get_tech(tech_id)));
                    string adv_name;
                    int tech_lv = tech_id % 4;
                    switch (adv_type)
                    {
                    case 0:
                        change_tech_gold(tech_id, tech_cost_gold[tech_id], true);
                        adv_name = "资金";
                        break;
                    case 1:
                        change_tech_tp(tech_id, tech_cost_tp[tech_id], true);
                        adv_name = "技巧点";
                        break;
                    case 2:
                        change_tech_time(tech_id, tech_cost_time[tech_id], true);
                        adv_name = "时间";
                        break;
                    }
                    if (force.is_player())
                    {
                        pk::message_box(pk::encode(pk::format("今天专程来帮你研究\x1b[1x{}\x1b[0x。这次就花费一半\x1b[1x{}\x1b[0x吧。\n要现在就开始研究哦，不然我可就不在了。", tech_name, adv_name)),
                                        pk::get_person(武将_司马徽));
                    }
                }
            }
        }
        //---------------------------------------------------------------------------------------
        void change_tech_gold(int tech_id, uint16 gold_cost, bool advantage)
        {
            pk::tech @tech = pk::get_tech(tech_id);
            uint16 cost = tech.gold_cost;
            tech.gold_cost = gold_cost / (advantage ? 2 : 1);
            if (调试模式)
                pk::info(pk::format("技巧{} 需要资金:{}{}", pk::decode(pk::get_name(tech)), cost, gold_cost));
        }
        void change_tech_tp(int tech_id, uint16 tp_cost, bool advantage)
        {
            pk::tech @tech = pk::get_tech(tech_id);
            uint16 cost = tech.tp_cost;
            tech.tp_cost = tp_cost / (advantage ? 2 : 1);
            if (调试模式)
                pk::info(pk::format("技巧{} 需要技巧点:{}{}", pk::decode(pk::get_name(tech)), cost, tp_cost));
        }
        void change_tech_time(int tech_id, uint16 time_cost, bool advantage)
        {
            pk::tech @tech = pk::get_tech(tech_id);
            uint16 cost = tech.time_cost;
            tech.time_cost = time_cost / (advantage ? 2 : 1) + (((time_cost % (advantage ? 2 : 1)) == 0) ? 0 : 1);
            if (调试模式)
                pk::info(pk::format("技巧{} 需要时间:{}{}", pk::decode(pk::get_name(tech)), cost, time_cost));
        }
        //---------------------------------------------------------------------------------------
        int get_next_tech_id(pk::force @force, int tech_group)
        {
            int tech_id = -1;
            for (int tech_lv = 0; tech_lv < 4; tech_lv++)
            {
                int tech_temp = 4 * tech_group + tech_lv;
                if (!pk::has_tech(force, tech_temp))
                {
                    tech_id = tech_temp;
                    return tech_id;
                }
            }
            return tech_id;
        }
        void backup_tech_cost()
        {
            for (int tech_id = 0; tech_id < 技巧_末; tech_id++)
            {
                pk::tech @tech = pk::get_tech(tech_id);
                tech_cost_gold[tech_id] = tech.gold_cost;
                tech_cost_tp[tech_id] = tech.tp_cost;
                tech_cost_time[tech_id] = tech.time_cost;
            }
        }
        void reset_tech_cost()
        {
            for (int tech_id = 0; tech_id < 技巧_末; tech_id++)
            {
                pk::tech @tech = pk::get_tech(tech_id);
                tech.gold_cost = tech_cost_gold[tech_id];
                tech.tp_cost = tech_cost_tp[tech_id];
                tech.time_cost = tech_cost_time[tech_id];
            }
        }
        //---------------------------------------------------------------------------------------
    } Main main;
}
